<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class BrandController extends Controller
{
    //品牌管理
    public function add(Request $request)
    {   //品牌首页
        $title = '商品管理';
        //会话控制
        $staff = $request->session()->get('staff');

        $data = DB::table('bro_cate')->where('pid', '=', '0')->get();
        $pid = DB::table('bro_cate')->where('pid', '=', $data[0]->id)->get();
        return view('admin.brand.add', ['title' => $title, 'pid' => $pid,'staff' => $staff]);
    }

    //品牌添加
    public function insert(Request $request)
    {
        //获取添加页面提交上来的数据,并去除了'_token'隐藏的数据'
        $data = $request->except('_token');


        if ($request->hasFile('brandlogo'))//判断是否有文件
        {
            if ($request->file('brandlogo')->isValid())//判断文件是否有效
            {
                $arr = array('jpg', 'png', 'gif');
                //获取文件的后缀名
                $suffix = $request->file('brandlogo')->getClientOriginalExtension();

                //判断上传文件是否是图片
                if (in_array($suffix, $arr)) {


                    $fileName = time() . rand() . '.' . $suffix;

                    $cate_id = $data['cate_id'];

                    $data['brandlogo'] = '/uploads/brand/' . $cate_id . '/' . $fileName;

                    //上传文件
                    $path = './uploads/brand/' . $cate_id;//上传文件的目录
                    //执行上传
                    $request->file('brandlogo')->move($path, $fileName);

                    //拼凑goods_pic的URL地址
//                        $data['goods_pic'] = trim($path.'/'.$category_id.'/'.$fileName,'.');

                    $res = DB::table('bro_brand')->insert($data);
                    return redirect('/admin/brand/show')->with('success', '添加成功');

                } else {

                    return redirect('/admin/brand/show')->with('success', '上传文件不合法');
                }
            }
        }

    }

    //品牌列表
    public function show(Request $request)
    {
        $title = '品牌列表';
        //会话控制
        $staff = $request->session()->get('staff');

        $data = DB::table('bro_brand') -> orderBy(DB::raw('RAND()')) -> paginate($request -> input('num',5));
//        dd($data);
//        dd(count($data));
            if(!empty($data))
            {
                for($i = 0; $i < count($data); $i++)
                {
                    $catename = DB::table('bro_cate') -> where('id',$data[$i] -> cate_id) -> first() -> catename;

                    $data[$i] -> catename = $catename;
                }
                return view('admin.brand.show', ['title' => $title, 'data' => $data,'staff' => $staff]);
            }
            return view('admin.brand.show', ['title' => $title, 'data' => $data,'staff' => $staff]);

    }

    //品牌编辑
    public function edit(Request $request,$id)
    {
        $title = '品牌修改';
        //会话控制
        $staff = $request->session()->get('staff');

        $data = DB::table('bro_cate')->where('pid', '=', '0')->get();
        $pid = DB::table('bro_cate')->where('pid', '=', $data[0]->id)->get();
        $data = DB::table('bro_brand')->where('id', '=', $id)->get();

        return view('admin.brand.edit', ['title' => $title, 'pid' => $pid, 'data' => $data,'staff'=>$staff]);
    }

    //品牌修改
    public function update(Request $request)
    {

        $data = $request->except('_token', 'oldpic');
        if ($request->hasFile('brandlogo'))//判断是否有文件
        {
            if ($request->file('brandlogo')->isValid())//判断文件是否有效
            {
                $arr = array('jpg', 'png', 'gif');
                //获取文件的后缀名
                $suffix = $request->file('brandlogo')->getClientOriginalExtension();
                //判断上传文件是否是图片
                if (in_array($suffix, $arr)) {
                    $fileName = time() . rand() . '.' . $suffix;

                    $cate_id = $data['cate_id'];

                    $data['brandlogo'] = '/uploads/brand/' . $cate_id . '/' . $fileName;

                    //上传文件
                    $path = './uploads/brand/' . $cate_id;//上传文件的目录
                    // dd($path);

                    //执行上传
                    $request->file('brandlogo')->move($path, $fileName);

                    $oldpath = '.' . $request->input('oldpic');
                    

                    if ($oldpath != '.') {
                        if (file_exists($oldpath)) {
                            unlink($oldpath);
                        }
                    }
                    $data = DB::table('bro_brand')->where('id', $data['id'])->update($data);
                }else{
                    return back() -> with('error','上传文件不合法');
                }
            }
        }
        if($data){
            return redirect('/admin/brand/show')->with('success', '修改成功');
        }
        
    }


    //品牌删除
    public function delete($id)
    {
        $brandlogo = DB::table('bro_brand') -> where('id',$id) -> first() -> brandlogo;

        $oldpath = '.'.$brandlogo;

        if($oldpath != '.')
        {
            if(file_exists($oldpath))
            {
                unlink($oldpath);
            }
        }
        $res = DB::table('bro_brand') -> where('id',$id) -> delete();
        //判断是否删除成功
        if($res)
        {
            return redirect('/admin/brand/show') -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }
}

